import $service from '../service';
import {
    encrypt,
    decrypt
} from '../utils/crypto.js'
const baseUrl = $service.baseUrl
const _header = {
    'miniToken': wx.getStorageSync('miniToken')
}
let isTesting = $service.getEnv() == 'develop';
let flag = true
export default {
    request(url, data, loading = false, method = 'POST') {
        let requestParams = {};
        //  请求加密
        requestParams = data || {}
        // if (!isTesting) {
        //     requestParams = data || {}
        // } else {
        //     let encryptStr = '';
        //     for (let it in data) {
        //         if (data[it]) {
        //             if (data[it] instanceof Array) {
        //                 data[it] = JSON.stringify(data[it])
        //             }
        //             encryptStr += `${it}=${data[it]}&`;
        //         }
        //     }
        //     requestParams = {
        //         content: encrypt(encryptStr.substr(0, encryptStr.length - 1)),
        //         isEncrypt: 1
        //     };
        // }
        return new Promise((resolve, reject) => {
            if (loading) {
                wx.showLoading({
                    title: '加载中',
                    mask: true
                })
            }
            wx.request({
                url: baseUrl + url,
                data: requestParams,
                header: wx.getStorageSync('miniToken') ? {
                    'miniToken': wx.getStorageSync('miniToken')
                } : {},
                method: method,
                success: (res => {
                    if (res.statusCode === 200) {
                        let res_data = $service.deepClone(res.data);
                        //    // 对返回数据进行解密
                        //    if (res_data) {
                        //     try {
                        //         res_data.data = typeof res.data.data == 'string' ? JSON.parse(decrypt(res_data.data)) : res_data.data;
                        //     } catch (err) {
                        //         reject({
                        //             code: 0,
                        //             msg: `执行数据解密失败！当前返回data类型为 ${typeof res.data.data}`
                        //         });
                        //     }
                        //    }
                        if (res_data.code === 1) {
                            resolve(res_data)
                        } else if (res_data.code === 2001) {
                            let options = getCurrentPages()[0].options
                            console.log(getCurrentPages()[0]);
                                console.log(flag);
                                if (getCurrentPages()[0].route.includes("/index_doctor_details")) {
                                    console.log(options,1);
                                    wx.clearStorage()
                                    wx.reLaunch({
                                        url: '/pages/login/login?doctorId=' + options.doctorId,
                                    })
                                }
                                else if (getCurrentPages()[0].route.includes("/my_doctor/index")) {
                                    console.log(options,1);
                                    wx.clearStorage()
                                    wx.reLaunch({
                                        url: '/pages/login/login?isFromMyDoctor=1'
                                    })
                                }
                                else if(getCurrentPages()[0].route.includes("/login")&&getCurrentPages()[0].query.doctorId){
                                    console.log(options,2);
                                    wx.reLaunch({
                                        url: '/pages/login/login?doctorId=' + options.doctorId,
                                    })
                                }
                                else {
                                    wx.clearStorage()
                                    wx.reLaunch({
                                        url: '/pages/login/login',
                                    })
                                }
                            $service.showToast(res_data.msg);
                        } else {
                            reject(res_data)
                        }
                    } else {
                        //其它错误，提示用户错误信息
                        reject(res_data)
                    }
                }),
                fail: (() => {
                    reject(res_data)
                }),
                complete: (() => {
                    wx.hideLoading()
                })
            })
        })
    },
}